<?php

class Of_Amazon_ResultSet implements Iterator 
{
  protected $xml;
  
  protected $cursor = 0;
  protected $end;
  
  public function __construct($xml)
  {
    $this->xml = $xml;
    
    if(@strpos($this->getElement()->Items->Request->IsValid, "True") === false)
    {
      throw new Zend_Service_Exception("アイテムの取得に失敗しました");
    }
    
    if(@($e = $this->getElement()->Items->Request->Errors->Error)instanceof SimpleXMLElement )
    {
      throw new Zend_Service_Exception($e->Message);
    }
    $this->end = count($this->getElement()->Items->Item);
    
  }
  
  /**
   * 
   * @return SimpleXmlElement
   */
  public function getElement()
  {
    return $this->xml;
  }
  
  /**
   * カーソル初期化
   * 
   */
  public function rewind(){
    $this->cursor = 0;
  }
  /**
   * 現在のキー
   */
  public function key(){
    return $this->cursor;
  }
  
  /**
   * 現在のオブジェクト
   * 
   */
  public function current(){
    
    return new Of_Amazon_Item($this->getElement()->Items->Item[$this->cursor]);
  }
  public function next(){
    
    $this->cursor++;
  }
  public function valid(){
    
    return ($this->cursor < $this->end);
  }


}